<?php

namespace app\admin\controller;
use app\base\controller\Base;
use think\Request;
use think\Cache;
use think\Config;
use think\Session;



class Privilege extends Base
{
    public  $model;

    public function __construct()
    {
   $this->model = model("Privilege");
        parent::__construct();
    $this->agent_parent_id = 1;//管理员顶级

    }



    public function index(){
        $where['is_config'] = 0;
        //权限管理
        $authResult = w_g("authResult");
        if($this->agents_ids!="all"){
                if(!empty($authResult)){
                    $where['id'] = ['in',$authResult];
                }else{
                    return   failJson("查询失败");
                }
        }


        $resback =  $res =    $this->model->Privilege_page($where);



      //  where("id","in",$authResult)



       $res = $res['res'];
        /**
         *  获取父级
         */
        $parentArr = [];
        foreach($res as $k=>$v){
            if($v['parent_id']==0){
                $parentArr[] = $v;
            }
        }
        $parentArrIter = $parentArr;
        //二级处理
        foreach($parentArrIter as $kp=>$vp){
           $t_arr =  filterPrivilege($res,$vp['id']);
           if(!empty($t_arr)){
               $parentArr[$kp]['children'] = $t_arr;
           }

        }
        //三级处理
        $parentArrIterThree = $parentArr;
        foreach($parentArrIterThree as $kps=>&$vps){
            if(isset($vps['children'])){
                foreach($vps['children'] as $kc=>&$vc){
                    $t_arr =  filterPrivilege($res,$vc['id']);
                    if(!empty($t_arr)){
                        $vc['children'] = $t_arr;
                    }
                }

            }

        }


        $resback['res']  = $parentArrIterThree;
        return   resJson(1,"查询成功",$resback['res'],$resback['pageInfo']);
    }

    public function save(){
        $where['is_config'] = 0;
        //权限管理
        $res =    $this->model->Privilege_page2($where);
        $res_temp = [];
        return   resJson(1,"查询成功",$res['res'],$res['pageInfo']);
    }

    //显示层权限列表
    public function listpri(){
        $authResult = w_g("authResult");
        $res = [];


        if($this->agents_ids == 'all'){
            $res = $this->model->where("is_nav",1)->order(" displayorder,id asc")->select();
        }else{
            if($authResult){
                $res = $this->model->where("is_nav",1)->where("id","in",$authResult)->order(" displayorder,id asc")->select();

            }

        }
        if(!empty($res)){
            return   resJson(1,"查询成功",$res);
        }
                    //显示全部
        return   failJson("查询失败");
    }









}
